package com.mogujie.tt.DB.sp;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

/**
 * Created by user on 2015/9/15.
 */
public class RegisterSp {
    private final String fileName = "register.ini";
    private Context ctx;
    private final String KEY_REGISTER_NAME = "registerName";
    private final String KEY_PWD = "pwd";

    private final String KEY_DETAILED_NICKNAME ="detailedNickName";
    private final String KEY_DETAILED_HEADPORTRAIT ="detailedHeadPortrait";
    private final String KEY_DETAILED_SEX = "detailedSex";
    private final String KEY_DETAILED_MOBILEPHONE ="detailedMobilePhone";
    private final String KEY_DETAILED_EMAIL ="detailedEmail";
    private final String KRY_DETATLED_SIGN ="detailedSign";


    SharedPreferences sharedPreferences;
    //单例
    private static RegisterSp registerSp = null;
    public static RegisterSp instance(){
        if(registerSp ==null){
            synchronized (RegisterSp.class){
                registerSp = new RegisterSp();
            }
        }
        return registerSp;
    }
    private RegisterSp(){
    }

    public void  init(Context ctx){
        this.ctx = ctx;
        sharedPreferences= ctx.getSharedPreferences
                (fileName,ctx.MODE_PRIVATE);
    }


    /**
     * 设置注册信息
     * @param userName
     * @param pwd
     */

    public  void setRegisterInfo(String userName,String pwd){
        // 横写
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(KEY_REGISTER_NAME, userName);
        editor.putString(KEY_PWD, pwd);
        //提交当前数据
        editor.commit();
    }

    /**
     * 得到注册的详细信息
     * @return
     */
    public SpRegisterIdentity getRegisterIdentity(){
        String userName =  sharedPreferences.getString(KEY_REGISTER_NAME, null);
        String pwd = sharedPreferences.getString(KEY_PWD, null);
        /**pwd不判空: loginOut的时候会将pwd清空*/
        if(TextUtils.isEmpty(userName) ){
            return null;
        }
        return new SpRegisterIdentity(userName,pwd);
    }

    public class SpRegisterIdentity{
        private String registerName;
        private String pwd;


        public SpRegisterIdentity(String mUserName,String mPwd){
            registerName = mUserName;
            pwd = mPwd;
        }

        public String getRegisterName() {
            return registerName;
        }

        public void setRegisterName(String registerName) {
            this.registerName = registerName;
        }

        public String getPwd() {
            return pwd;
        }

        public void setPwd(String pwd) {
            this.pwd = pwd;
        }

    }

    /**
     * 设置用户的详细信息
     */
    public void setDetailedInfo(String detailedHeadPortrait, String detailedNickName, int detailedSex, String detailedMobilePhone, String detailedEmail ,String detailedSign){
        // 横写
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(KEY_DETAILED_HEADPORTRAIT, detailedHeadPortrait);
            editor.putString(KEY_DETAILED_NICKNAME, detailedNickName);
            editor.putString(KEY_DETAILED_MOBILEPHONE, detailedMobilePhone);
            editor.putString(KEY_DETAILED_EMAIL, detailedEmail);
            editor.putInt(KEY_DETAILED_SEX, detailedSex);
            editor.putString(KRY_DETATLED_SIGN,detailedSign);
        //提交当前数据
        editor.commit();

    }
    //设置头像
    public void setdetailedHeadPortraitStrConfig( String value) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(KEY_DETAILED_HEADPORTRAIT, value);
        //提交当前数据
        editor.commit();
    }
    //设置昵称
    public void setdetailedNickNameStrConfig( String value) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(KEY_DETAILED_NICKNAME, value);
        //提交当前数据
        editor.commit();
    }
    //设置性别
    public void setdetailedSexIntConfig( int value) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(KEY_DETAILED_SEX, value);
        //提交当前数据
        editor.commit();
    }
    //设置手机号码
    public void setdetailedMobilePhoneStrConfig( String value) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(KEY_DETAILED_MOBILEPHONE, value);
        //提交当前数据
        editor.commit();
    }

    //设置手机号码
    public void setdetailedEmailStrConfig( String value) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(KEY_DETAILED_EMAIL, value);
        //提交当前数据
        editor.commit();
    }

    //设置个性签名
    public void setdetailedSignStrConfig( String value) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(KRY_DETATLED_SIGN, value);
        //提交当前数据
        editor.commit();
    }


    /**
     * 得到用户的详细信息
     */
    public SpDetailedInfo getSpDetailedInfo(){
        String detailedHeadPortrait = sharedPreferences.getString(KEY_DETAILED_HEADPORTRAIT, null);
        String detailedNickName =  sharedPreferences.getString(KEY_DETAILED_NICKNAME, null);
        int detailedSex = sharedPreferences.getInt(KEY_DETAILED_SEX, 0);
        String detailedMobilePhone = sharedPreferences.getString(KEY_DETAILED_MOBILEPHONE, null);
        String detailedEmail = sharedPreferences.getString(KEY_DETAILED_EMAIL, null);
        String detailedSign = sharedPreferences.getString(KRY_DETATLED_SIGN,null);

        return new SpDetailedInfo(detailedHeadPortrait,detailedNickName,detailedSex,detailedMobilePhone,detailedEmail,detailedSign);
    }
    public class SpDetailedInfo{
        private String detailedNickName;
        private String detailedHeadPortrait;
        private int detailedSex;
        private String detailedMobilePhone;
        private String detailedEmail;
        private String detailedSign;

        public SpDetailedInfo(String detailedHeadPortrait, String detailedNickName, int detailedSex, String detailedMobilePhone, String detailedEmail, String detailedSign) {
            this.detailedHeadPortrait = detailedHeadPortrait;
            this.detailedNickName = detailedNickName;
            this.detailedSex = detailedSex;
            this.detailedMobilePhone = detailedMobilePhone;
            this.detailedEmail = detailedEmail;
            this.detailedSign =detailedSign;

        }

        public String getDetailedNickName() {
            return detailedNickName;
        }

        public void setDetailedNickName(String detailedNickName) {
            this.detailedNickName = detailedNickName;
        }

        public String getDetailedHeadPortrait() {
            return detailedHeadPortrait;
        }

        public void setDetailedHeadPortrait(String detailedHeadPortrait) {
            this.detailedHeadPortrait = detailedHeadPortrait;
        }

        public int getDetailedSex() {
            return detailedSex;
        }

        public void setDetailedSex(int detailedSex) {
            this.detailedSex = detailedSex;
        }

        public String getDetailedMobilePhone() {
            return detailedMobilePhone;
        }

        public void setDetailedMobilePhone(String detailedMobilePhone) {
            this.detailedMobilePhone = detailedMobilePhone;
        }

        public String getDetailedEmail() {
            return detailedEmail;
        }

        public void setDetailedEmail(String detailedEmail) {
            this.detailedEmail = detailedEmail;
        }

        public String getDetailedSign() {return detailedSign;}

        public void setDetailedSign(String detailedSign) {this.detailedSign = detailedSign;}
    }


}
